package droid.frame.activity.base;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.Window;

import java.util.List;

import droid.frame.activity.title.AppTitle;
import droid.frame.activity.title.TitleMgr;
import droid.frame.utils.android.Log;

/**
 * 
 * @author coffee <br>
 *         2016-3-15下午2:54:44
 */
public class BaseActivity extends FrameBaseActivity {

	private AppTitle appTitle;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		// setSystemBar(null);
	}

	protected void onCreateNoSystemBar(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void findViewById() {
		appTitle = new TitleMgr(this);
		appTitle.initTitle();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	public AppTitle getAppTitle() {
		return appTitle;
	}

	public void setAppTitle(AppTitle appTitle) {
		this.appTitle = appTitle;
	}

	@SuppressLint("RestrictedApi")
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		FragmentManager fm = getSupportFragmentManager();
		int index = requestCode >> 16;
		if (index != 0) {
			index--;
			if (fm.getFragments() == null || index < 0
					|| index >= fm.getFragments().size()) {
				Log.d("BaseActivity2", "Activity result fragment index out of range: 0x" + Integer.toHexString(requestCode));
				return;
			}
			Fragment frag = fm.getFragments().get(index);
			if (frag == null) {
				Log.d("BaseActivity2", "Activity result no fragment exists for index: 0x"
						+ Integer.toHexString(requestCode));
			} else {
				handleResult(frag, requestCode, resultCode, data);
			}
			return;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	/**
	 * 递归调用，对所有子Fragement生效
	 *
	 * @param frag
	 * @param requestCode
	 * @param resultCode
	 * @param data
	 */
	private void handleResult(Fragment frag, int requestCode, int resultCode,
							  Intent data) {
		frag.onActivityResult(requestCode & 0xffff, resultCode, data);
		@SuppressLint("RestrictedApi")
		List<Fragment> frags = frag.getChildFragmentManager().getFragments();
		if (frags != null) {
			for (Fragment f : frags) {
				if (f != null)
					handleResult(f, requestCode, resultCode, data);
			}
		}
	}
	/**
	 * 显示加载进度对话框<br>
	 * 一般网络请求的时候用到, 每个Activity都会持有一个Dialog对象
	 * 
	 * @param message
	 */
//	protected void showLoadingDialog(String message) {
//		if (mLoadingDialog != null) {
//			mLoadingDialog.setMessage(message);
//		} else {
//			mLoadingDialog = new AppLoading(getContext());
//			mLoadingDialog.setMessage(message);
//			mLoadingDialog.show();
//		}
//	}
//
//	/**
//	 * 取消对话框
//	 */
//	protected void cancelLoadingDialog() {
//		if (mLoadingDialog != null) {
//			mLoadingDialog.cancel();
//		}
//	}

}
